﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{
    public class DownTimeAddressDbData : SubscriptionDbDataBase
    {
        public DownTimeAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
        }
        public override void CreateSubscription()
        {
            try
            {
                opcServer.QuickAddMonitorItem(opcNodes.DowntimeAddress.GrpoupName,
                    opcNodes.DowntimeAddress.PublishTime,
                    opcNodes.DowntimeAddress.SampleTime,
                    opcNodes.DowntimeAddress.GetNodeAddress(),
                    opcNodes.DowntimeAddress.GetNodeDisplayName(),
                    SubscriptionHandle_DowntimeAddress);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("DownTimeAddressDbData CreateSubscription", ex);
                throw ex;
            }

        }

        public event EventHandler<MachineStatusChangedEventArgs> MachineStatusChanged;

        //向L3 汇报 DOWNTIME 
        //1. 从L1 获取一个DOWNTIME 后 状态为 0：CREATED
        //2. DOWNTIME_ID 为OPC 读取时刻:yymmddhhmmss
        //3. 同时会获取 statrt time
        //4. 后面每次对这个故障的内容修改都为 1: update

        private void SubscriptionHandle_DowntimeAddress(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
            if (monitoredItem.DisplayName == "Status")
            {
                string downTimeStatus = notification.Value.Value.ToString();

                //代表这个是一个故障的恢复
                if (downTimeStatus.Equals("0"))
                {
                    try
                    {
                        //Console.WriteLine("Millline reset button triggerd ");
                        LogHelper.WriteLog("Millline reset button triggerd");
                        DOWNTIME_TABLE.UpdateDownTimeRecord(DateTime.Now);
                        //故障的恢复以auto start 信号为准
                        //MachineStatusChanged?.Invoke(this, new MachineStatusChangedEventArgs(true));
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("SubscriptionHandle_DowntimeAddress", ex);
                    }

                }

                //代表是个新故障
                else
                {
                    try
                    {
                        //Console.WriteLine("Millline downTime occured ");
                        LogHelper.WriteLog("Millline downTime occured ");
                        DOWNTIME_TABLE.CreateNewDownTimeRecord(DateTime.Now, downTimeStatus);
                        MachineStatusChanged?.Invoke(this, new MachineStatusChangedEventArgs(false));

                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("SubscriptionHandle_DowntimeAddress", ex);
                    }

                }
            }
        }
    }
}
